package ui.swing.tree;

import java.awt.Color;
import java.awt.Font;
import java.util.Enumeration;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;

public class TreeFromTreeModelDemo extends JFrame {

	private static final long serialVersionUID = -278034040905550629L;

	public TreeFromTreeModelDemo() {
		super("用TreeModel建树,还包括用CellRenderer设置一些风格");
		setBounds(200,200,300,400);

		Font font = new Font("Dialog", Font.PLAIN, 12);
		Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
		/** 定义Windows风格界面 **/
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			if (UIManager.get(key) instanceof Font) {
				UIManager.put(key, font);
			}
		}
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception el) {
			System.exit(0);
		}
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");
		DefaultMutableTreeNode node1=new DefaultMutableTreeNode("我的公文包");
		DefaultMutableTreeNode node2=new DefaultMutableTreeNode("我的电脑");
		DefaultMutableTreeNode node3=new DefaultMutableTreeNode("网站");
		DefaultMutableTreeNode node4=new DefaultMutableTreeNode("Readme");
		
		DefaultTreeModel treeModel = new DefaultTreeModel(root);
		/* DefaultTreeModel类所提供的insertNodeInto()方法加入节点到父节点的数量.
         * 利用DefaultMutableTreeNode类所提供的getChildCount()方法取得目前子节点的数量.
         */
		treeModel.insertNodeInto(node1, root, root.getChildCount());
		treeModel.insertNodeInto(node2, root, root.getChildCount());
		treeModel.insertNodeInto(node3, root, root.getChildCount());
		treeModel.insertNodeInto(node4, root, root.getChildCount());

		DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode("公司文件");
		treeModel.insertNodeInto(leafNode, node1, node1.getChildCount());
		//node1.add(leafNode);
		leafNode = new DefaultMutableTreeNode("私人文件");
		treeModel.insertNodeInto(leafNode, node1, node1.getChildCount());
		//node1.add(leafNode);
		leafNode = new DefaultMutableTreeNode("个人信件");
		treeModel.insertNodeInto(leafNode, node1, node1.getChildCount());
		//node1.add(leafNode);
		
		leafNode = new DefaultMutableTreeNode("本机磁盘(c:)");
		treeModel.insertNodeInto(leafNode, node2, node2.getChildCount());
		//node2.add(leafNode);
		leafNode = new DefaultMutableTreeNode("本机磁盘(d:)");
		treeModel.insertNodeInto(leafNode, node2, node2.getChildCount());
		//node2.add(leafNode);
		leafNode = new DefaultMutableTreeNode("本机磁盘(e:)");
		treeModel.insertNodeInto(leafNode, node2, node2.getChildCount());
		//node2.add(leafNode);
		
		DefaultMutableTreeNode cNode = new DefaultMutableTreeNode("搜索");
		treeModel.insertNodeInto(new DefaultMutableTreeNode("百度"), cNode, cNode.getChildCount());
		treeModel.insertNodeInto(new DefaultMutableTreeNode("Google"), cNode, cNode.getChildCount());
		treeModel.insertNodeInto(new DefaultMutableTreeNode("必应"), cNode, cNode.getChildCount());
		treeModel.insertNodeInto(cNode, node3, node3.getChildCount());
		cNode = new DefaultMutableTreeNode("门户");
		treeModel.insertNodeInto(new DefaultMutableTreeNode("新浪"), cNode, cNode.getChildCount());
		treeModel.insertNodeInto(new DefaultMutableTreeNode("搜狐"), cNode, cNode.getChildCount());
		treeModel.insertNodeInto(new DefaultMutableTreeNode("网易"), cNode, cNode.getChildCount());
		treeModel.insertNodeInto(cNode, node3, node3.getChildCount());
		
		JTree tree = new JTree(treeModel);

		// 改变JTree的外观 
		//tree.putClientProperty("JTree.lineStyle","None");
        //tree.putClientProperty("JTree.lineStyle","Horizontal");
		//tree.putClientProperty("JTree.lineStyle","Angled");
		
		// 设置行高
		tree.setRowHeight(20);
		
		// 取得默认的TreeCellRenderer来改变一些外观
		DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
		cellRenderer.setLeafIcon(new ImageIcon(getClass().getResource("/resources/images/red.gif")));
		cellRenderer.setOpenIcon(new ImageIcon(getClass().getResource("/resources/images/yellow.gif")));
		cellRenderer.setClosedIcon(new ImageIcon(getClass().getResource("/resources/images/green.gif")));
		
		cellRenderer.setFont(new Font("宋体", Font.PLAIN, 12));
		//cellRenderer.setBackgroundNonSelectionColor(Color.WHITE);
		cellRenderer.setBackgroundSelectionColor(Color.BLUE);
		cellRenderer.setBorderSelectionColor(Color.BLACK);
		
		JScrollPane jsp = new JScrollPane();
		jsp.setViewportView(tree);
		
		getContentPane().add(jsp);
		//pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new TreeFromTreeModelDemo();
	}
	
}
